package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import top.maof.book.dao.UserRoleMapper;
import top.maof.book.model.UserRole;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.service.UserRoleService;
import top.maof.book.utils.Utils;

import java.util.List;

/**
 * Service实现
 *
 * @author 久念
 * @date 2022/03/03 19:37
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public Integer create(UserRole userRole) {
        userRoleMapper.insert(userRole);
        return userRole.getId();
    }

    @Override
    public void deleteById(Integer id) {
        userRoleMapper.deleteById(id);
    }

    @Override
    public void delete(UserRole userRole) {
        UpdateWrapper<UserRole> deleteWrapper = new UpdateWrapper<>(userRole);
        userRoleMapper.delete(deleteWrapper);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        userRoleMapper.deleteBatchIds(ids);
    }


    @Override
    public void updateByIdInBatch(List<UserRole> userRoles) {
        if (CollectionUtils.isEmpty(userRoles)) {
            return;
        }
        for (UserRole userRole : userRoles) {
            this.updateById(userRole);
        }
    }

    @Override
    public UserRole findById(Integer id) {
        return userRoleMapper.selectById(id);
    }

    @Override
    public UserRole findOne(UserRole userRole) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>(userRole);
        return userRoleMapper.selectOne(wrapper);
    }

    @Override
    public List<UserRole> findList(UserRole userRole) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>(userRole);
        return userRoleMapper.selectList(wrapper);
    }

    @Override
    public PageData<UserRole> findPage(PageWrap<UserRole> pageWrap) {
        IPage<UserRole> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(UserRole::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getUserId() != null) {
            queryWrapper.lambda().eq(UserRole::getUserId, pageWrap.getModel().getUserId());
        }
        if (pageWrap.getModel().getRoleId() != null) {
            queryWrapper.lambda().eq(UserRole::getRoleId, pageWrap.getModel().getRoleId());
        }
        for (PageWrap.SortData sortData : pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(userRoleMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(UserRole userRole) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>(userRole);
        return userRoleMapper.selectCount(wrapper);
    }
}
